﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using System.Drawing;
using Silvermoon.Visuals;
using Silvermoon.Utils;
using Silvermoon.Animations;

namespace Silvermoon.Controls
{
    public class ImageToolbar : ToolbarBase
    {
        #region fields

        private Color color = SysColors.HeaderColor;
        private ImageButton[] buttons;
        private BackgroundVisual background;
        private int? backgroundHeight;
        private Size buttonSize = new Size(72, 72);

        #endregion
        #region ctor

        public ImageToolbar()
        {
            buttons = new ImageButton[5];
            for (int i = 0; i < buttons.Length; i++)
            {
                ImageButton b = new ImageButton { Size = buttonSize };
                buttons[i] = b;
            }
            Height = ToolbarHeight;

            background = new BackgroundVisual { Colors = ColorUtil.CreateChromeColors(SysColors.HeaderColor), Style = RenderStyle.Chrome };
            Visuals = new Visual[] { background };
        }

        #endregion
        #region props
        /// <summary>
        /// Gets the buttons on the toolbar.
        /// </summary>
        public ImageButton[] Buttons { get { return buttons; } }

        public Size ButtonSize
        {
            get { return buttonSize; }
            set
            {
                if (buttonSize != value)
                {
                    buttonSize = value;

                    Invalidate(InvalidationFlag.LayoutAppearance);
                    OnSizeChanged();
                    if (Window != null) Window.Invalidate(InvalidationFlag.LayoutAppearance);
                }
            }
        }

        public Color Color
        {
            get { return color; }
            set
            {
                if (color != value)
                {
                    color = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        /// <summary>
        /// Gets or sets height of the background.
        /// </summary>
        public int BackgroundHeight
        {
            get { return backgroundHeight ?? Height; }
            set
            {
                if (value < 0) value = 0;
                else if (value > Height) value = Height;
                if (backgroundHeight != value)
                {
                    backgroundHeight = value;
                    Invalidate(InvalidationFlag.LayoutAppearance);
                }
            }
        }

        /// <summary>
        /// Gets the size of the overlay.
        /// </summary>
        /// <value>The size of the overlay.</value>
        protected override int OverlaySize { get { return Height - BackgroundHeight; } }

        protected internal override IList<Control> SubControls { get { return buttons; } }

        #endregion
        #region methods

        protected int ToolbarHeight { get { return buttonSize.Height + 4; } }

        protected override void OnSizeChanged()
        {
            if (Height != ToolbarHeight) Height = ToolbarHeight; else base.OnSizeChanged();
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Color))
            {
                background.Colors = ColorUtil.CreateChromeColors(color);
            }
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Margin | InvalidationFlag.LayoutAppearance))
            {
                background.Bounds = Control.ScaledRect(0, OverlaySize, Width, Height - OverlaySize);
                int distance = Width / 5;
                int x = (distance - buttonSize.Width) / 2;
                int y = (Height - buttonSize.Height) / 2;
                foreach (ImageButton b in buttons)
                {
                    b.Location = new Point(x, y);
                    b.Size = buttonSize;
                    x += distance;
                }
            }
            base.OnLayout();
        }

        public void SetMenu(int buttonIndex, ContextMenu menu)
        {
            if (buttonIndex < 0 || buttonIndex >= buttons.Length) throw new ArgumentOutOfRangeException("buttonIndex");

            ImageButton button = buttons[buttonIndex];
            button.Toggle = menu != null;
            if (menu != null)
            {
                button.DownChanged += (s, e) => { if (button.IsDown) menu.Show(button); else menu.Hide(TransitionMode.FadeOut | TransitionMode.SwipeOutToBottom); };
                menu.Showing += (s, e) => button.IsDown = true;
                menu.Hidden += (s, e) => button.IsDown = false;
            }
        }

        #endregion
    }
}
